﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Collector.EventHandle;

namespace Collector
{
    public partial class SubItemView : UserControl
    {
        public int SubItemIndex = 0;
        private bool bChangeColor = false;
       
        public SubItemView(int index)
        {
            InitializeComponent();
            SubItemIndex = index;

            InitControls();
            
            AppEvent.RegistEvent(EventId.DutSelect, OnDutSelect);
            AppEvent.RegistEvent(EventId.TestStart, OnTestStart);
            AppEvent.RegistEvent(EventId.AddLog, OnAddLog);
        }

        private void OnAddLog(object sender, AppEventArgs e)
        {
            if (!IsHandleCreated)
            {
                return;
            }
            //if ((int)e.SubId != SubItemIndex)
            //{
            //    return;
            //}
            string Msg = "";
            this.Invoke(new MethodInvoker(delegate
            {
                switch (e.State)
                {
                    case EventState.LOG_PROGRESS:
                        {
                            return;
                        }
                    case EventState.LOG_INFO:
                        {
                            Msg = e.Data as string;
                            if (null == Msg || Msg.Length <= 0)
                            {
                                return;
                            }
                        }
                        break;
                    case EventState.LOG_ITEM:
                    {
                        UIItemInfo uiinfo = e.Data as UIItemInfo;
                        Msg = uiinfo.MakeString();
                        listView_TestItem.BeginUpdate();
                        ListViewItem lvi = new ListViewItem();

                        lvi.SubItems[0].Text = (listView_TestItem.Items.Count + 1).ToString();
                        lvi.SubItems.Add(uiinfo.ItemName);
                        lvi.SubItems.Add(uiinfo.info);
                        //lvi.SubItems.Add(uiinfo.LimitLow);
                        lvi.SubItems.Add(uiinfo.TestValue);
                        //lvi.SubItems.Add(uiinfo.LimitHigh);
                        //lvi.SubItems.Add(uiinfo.TestUnit);
                        lvi.SubItems.Add(uiinfo.ItemResult);

                        if (bChangeColor)
                        {
                            lvi.BackColor = Color.LightBlue;
                        }

                        bChangeColor = !bChangeColor;
                        if (uiinfo.ItemResult.Trim().ToUpper() == "FAIL")
                        {
                            lvi.BackColor = Color.Red;
                        }
                        listView_TestItem.Items.Add(lvi);
                        listView_TestItem.EndUpdate();
                    }
                        break;
                    default:
                        //do nothing;
                        break;
                }
                Msg = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] " + Msg.Trim(); 
                //textBox_Log.AppendText(Msg + "\r\n");
            }));
        }
        private void InitControls()
        {
            listView_TestItem.Columns.Clear();
            listView_TestItem.Items.Clear();

            int unit = (Screen.PrimaryScreen.Bounds.Width - 100) / 10;

            ColumnHeader header1 = new ColumnHeader();
            header1.Width = unit / 2;
            header1.Text = LogKeys.GetInfo(ViewParam.ID).name;
            header1.TextAlign = HorizontalAlignment.Left;
            listView_TestItem.Columns.Add(header1);

            ColumnHeader header2 = new ColumnHeader();
            header2.Width = unit * 2;
            header2.Text = LogKeys.GetInfo(ViewParam.ItemName).name;
            header2.TextAlign = HorizontalAlignment.Left;
            listView_TestItem.Columns.Add(header2);

            ColumnHeader header3 = new ColumnHeader();
            header3.Width = unit * 3 + unit / 2;
            header3.Text = LogKeys.GetInfo(ViewParam.ItemInfo).name;
            header3.TextAlign = HorizontalAlignment.Left;
            listView_TestItem.Columns.Add(header3);

            //ColumnHeader header4 = new ColumnHeader();
            //header4.Width = unit * 1;
            //header4.Text = LogKeys.GetInfo(ViewParam.LimitLow).name;
            //header4.TextAlign = HorizontalAlignment.Left;
            //listView_TestItem.Columns.Add(header4);

            ColumnHeader header5 = new ColumnHeader();
            header5.Width = unit * 1;
            header5.Text = LogKeys.GetInfo(ViewParam.TestValue).name;
            header5.TextAlign = HorizontalAlignment.Left;
            listView_TestItem.Columns.Add(header5);

            //ColumnHeader header6 = new ColumnHeader();
            //header6.Width = unit * 1;
            //header6.Text = LogKeys.GetInfo(ViewParam.LimitHigh).name;
            //header6.TextAlign = HorizontalAlignment.Left;
            //listView_TestItem.Columns.Add(header6);

            //ColumnHeader header7 = new ColumnHeader();
            //header7.Width = unit * 1;
            //header7.Text = LogKeys.GetInfo(ViewParam.TestUnit).name;
            //header7.TextAlign = HorizontalAlignment.Left;
            //listView_TestItem.Columns.Add(header7);

            ColumnHeader header8 = new ColumnHeader();
            header8.Width = unit * 1;
            header8.Text = LogKeys.GetInfo(ViewParam.ItemResult).name;
            header8.TextAlign = HorizontalAlignment.Left;
            listView_TestItem.Columns.Add(header8);

            ImageList iList = new ImageList();
            iList.ImageSize = new Size(1, 30);
            listView_TestItem.SmallImageList = iList;

            //textBox_Log.Clear();
            bChangeColor = false;

        }
        private void OnDutSelect(object sender, AppEventArgs e)
        {
            if ((int)e.SubId != SubItemIndex)
            {
                return;
            }

            this.BringToFront();
        }
        
        private void OnTestStart(object sender, AppEventArgs e)
        {
            if ((int)e.SubId != SubItemIndex)
            {
                return;
            }
            
            this.Invoke(new MethodInvoker(delegate
            {
                InitControls();
            }));
        }
    }
}
